/**
 * @description 配置数据角色
 * @author 李艳
 */

import React, {Component} from 'react'
import { message } from 'antd';
import connect from "react-redux/es/connect/connect";
import {show, hide} from '../../../configs/SharedReducer'
import TransferTable from "../../../commons/components/TransferTable";
import {
    insertDataRoles,
    listAllAssignedDataRolesByPosition, listAllCanAssignDataRolesByPosition,
    listAllCanAssignDataRolesByPositionAndRoleGroup,
    listAllCanAssignEmployeesIncludeSubNode, listAllCanAssignFeatureRolesByPositionAndRoleGroup, removeDataRoles,
} from "./PositionService";
import {dataRoleGroupConfig, organizationConfig} from "../../../configs/CommonComponentsConfig";

class ConfigDataRole extends Component {
    handleLeftClick=async (rows)=> {
        let ids = [];
        const {positionId}=this.props;
        for (let i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        await removeDataRoles({parentId:positionId,childIds:ids}).then((data) => {
          const { status, message: msg } = data;
          if (status === 'SUCCESS') {
            message.success(msg);
          }
        }).catch(err => {
            this.props.hide()
        })

    }

    handleRightClick=async (rows)=> {
        let ids = [];
        const {positionId}=this.props;
        for (let i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        await insertDataRoles({parentId:positionId,childIds:ids}).then((data) => {
          const { status, message: msg } = data;
          if (status === 'SUCCESS') {
            message.success(msg);
          }
        }).catch(err => {
            this.props.hide()
        })
    };

    leftService=async(params)=> {
        let result = [];
        const {positionId}=this.props;
        await listAllCanAssignDataRolesByPosition({parentId:positionId}).then((res) => {
            result = res;
        })
        return result;

    };

    rightService=async (params)=> {
        let result = [];
        const {positionId}=this.props;
        await listAllAssignedDataRolesByPosition({parentId:positionId}).then((res) => {
            result = res
        });
        return result;

    };
    async JointQueryService(key) {
        let result = []
        const {positionId}=this.props;
        let params={};
        params.dataRoleGroupId=key;
        params.positionId=positionId;
        await listAllCanAssignDataRolesByPositionAndRoleGroup(params).then((res) => {
            result = res
        });
        return result;
    };
    render() {
        const leftColumns = [
            {
                title: '角色代码',
                dataIndex: 'code',
            },
            {
                title: '角色名称',
                dataIndex: 'name',
            },
            {
                title: '公共角色组织机构',
                dataIndex: 'publicOrg.name',
            },
            {
                title: '公共角色用户类型',
                dataIndex: 'userTypeRemark',
            }
        ];
        const searchTableConfig={...dataRoleGroupConfig,lable:"数据角色组"};
        return (
            <TransferTable
                handleLeftClick={this.handleLeftClick}
                handleRightClick={this.handleRightClick}
                searchTableConfig={searchTableConfig}
                rightService={this.rightService.bind(this)}
                leftService={this.leftService.bind(this)}
                JointQueryService={this.JointQueryService.bind(this)}
                leftColumns={leftColumns}
                rightColumns={leftColumns}
                updateLeftByJointQueryService={true}
            />

        );
    }


}

const mapStateToProps = (state) => {
    return {};
}

const mapDispatchToProps = (dispatch) => {
    return {
        show: () => {
            dispatch(show())
        },
        hide: () => {
            dispatch(hide())
        }
    }
}

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(ConfigDataRole)


